﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lib.Utils;
using Lib.Bases;

namespace MS.INV.MixedDrink
{
    public partial class Pc_DinhMuc : Lib.Bases.Forms.BaseForm
    {
        SysObj.Pc_DinhMuc dm = new SysObj.Pc_DinhMuc();

        DataTable DT_HH = new DataTable();

        public Pc_DinhMuc()
        {
            InitializeComponent();
        }

        protected override void OnHandleDestroyed(EventArgs e)
        {
            dm.Dispose();
            base.OnHandleDestroyed(e);
        }

        private void Pc_DinhMuc_Load(object sender, EventArgs e)
        {
            using (SysObj.L_HangHoa hh = new SysObj.L_HangHoa())
            {
                DT_HH = hh.LoadData();
                cbHH.Properties.SetDataSource(hh.ListMenu(), "MaHang,TenHang#Mã,Tên");
            }

            repHH.SetDataSource(DT_HH, "MaHang,TenHang#Mã,Tên");

            using (SysLst.L_DVT dvt = new SysLst.L_DVT())
            {
                repDVT.DataSource = dvt.LoadData();
            }
        }

        private void LoadHistory()
        {
            tree.Nodes.Clear();
            DataTable DT = dm.GetHistoryByMenuID(Cvert.ToInt32(cbHH.EditValue, 0));

            if (DT == null || DT.Rows.Count <= 0) return;

            foreach (DataRow ro in DT.Rows)
            {
                TreeNode tn = new TreeNode(Convert.ToDateTime(ro["Ngay"]).ToString("dd/MM/yyyy"));
                tn.Tag = Cvert.ToInt32(ro["IdDinhMuc"], 0);
                tree.Nodes.Add(tn);
            }
        }

        private void cbHH_EditValueChanged(object sender, EventArgs e)
        {
            btUp.Enabled = true;

            DataRow[] rS = DT_HH.Select("IdHangHoa=" + Cvert.ToInt32(cbHH.EditValue, 0));
            if (!object.Equals(rS, null) && rS.Length > 0)
            {
                txtDVT.Text = Cvert.ToString(rS[0]["TenDVT"], "");
            }

            LoadHistory();
            LoadData();
        }

        private void LoadData()
        {
            if (Cvert.ToInt32(cbHH.EditValue, 0) > 0)
            {
                dm.GetNewestByMenuID(Cvert.ToInt32(cbHH.EditValue, 0));
                dtNgay.EditValue = dm.Ngay;
                txtDG.Text = dm.DienGiai;
                grid.DataSource = dm.GetDetail();
            }
        }

        private void dtNgay_EditValueChanged(object sender, EventArgs e)
        {
            btUp.Enabled = true;

            if (Cvert.ToInt32(cbHH.EditValue, 0) > 0)
            {
                dm.Ngay = Convert.ToDateTime(dtNgay.EditValue);
                dm.IdThucDon = Cvert.ToInt32(cbHH.EditValue, 0);
                dm.ReadData();

                txtDG.Text = dm.DienGiai;
                grid.DataSource = dm.GetDetail();
            }
        }

        private void view_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            
        }

        private bool IsExists(int ProductID)
        {
            DataTable table = grid.DataSource as DataTable;
            DataRow[] rS = table.Select("IdHangHoa=" + ProductID);
            if (!object.Equals(rS, null) && rS.Length > 0) return true;
            return false;
        }

        private void btUp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (object.Equals(dtNgay.EditValue, null))
                {
                    Msg.ErrOK("Vui Lòng chọn ngày áp dụng trước khi cập nhật.");
                    return;
                }
                if (Cvert.ToInt32(cbHH.EditValue, 0) <= 0)
                {
                    Msg.ErrOK("Vui Lòng chọn tên thực đơn trước khi cập nhật.");
                    return;
                }

                DataTable DT = grid.GetAcceptChanges();
                if (DT != null) DT.AcceptChanges();

                dm.Ngay = Convert.ToDateTime(dtNgay.EditValue);
                dm.IdThucDon = Cvert.ToInt32(cbHH.EditValue, 0);
                dm.DienGiai = txtDG.Text;
                dm.D_HH = DT;

                dm.OnUpdate();

                LoadHistory();
                btUp.Enabled = false;
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Save data");
            }
        }

        private void btDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Msg.YesNoWarn("Bạn có chắc chắn muốn xóa định mức pha chế của thực đơn này không?") == DialogResult.No)
                return;

            try
            {
                dm.Ngay = Convert.ToDateTime(dtNgay.EditValue);
                dm.IdThucDon = Cvert.ToInt32(cbHH.EditValue, 0);
                dm.OnDelete();

                LoadHistory();
                LoadData();
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Delete data");
            }
        }

        private void btClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void dtNgay_EditValueChanged_1(object sender, EventArgs e)
        {
            btUp.Enabled = true;
        }

        private void grid_CellValueChanged(DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            btUp.Enabled = true;
            if (object.Equals(e, null)) return;

            if (e.Column.FieldName.ToLower() == "idhanghoa")
            {
                DataRow row = view.GetFocusedDataRow();
                if (object.Equals(row, null)) return;

                if (IsExists(Convert.ToInt32(row["IdHangHoa"])))
                {
                    MessageBox.Show("Thực đơn này đã tồn tại, vui lòng chọn thực đơn khác.", "Thông báo.",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    row["IdHangHoa"] = 0;
                    return;
                }

                DataRow[] rS = DT_HH.Select("IdHangHoa =" + row["IdHangHoa"].ToString());
                if (!object.Equals(rS, null) && rS.Length > 0)
                {
                    row["MaHang"] = rS[0]["MaHang"];
                    row["IdDVT"] = rS[0]["IdDVT"];
                    view.UpdateCurrentRow();
                }
            }
        }

        private void tree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            dm.ReadDataByID(Cvert.ToInt32(e.Node.Tag, 0));

            dtNgay.EditValue = dm.Ngay;
            cbHH.EditValue = dm.IdThucDon;
            txtDG.Text = dm.DienGiai;

            grid.DataSource = dm.GetDetail();
        }
    }
}
